package com.cluster.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cluster.platform.common.api.CommonPage;
import com.cluster.platform.common.api.CommonResult;
import com.cluster.platform.dto.CmsTagsInfoDto;
import com.cluster.platform.mapper.CmsTagsInfoMapper;
import com.cluster.platform.model.CmsTagsInfo;
import com.cluster.platform.service.CmsTagsInfoService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: CmsTagsInfoServiceImpl
 */
@Service
public class CmsTagsInfoServiceImpl implements CmsTagsInfoService {

    @Autowired
    private CmsTagsInfoMapper cmsTagsInfoMapper;

    @Override
    public CommonResult<Object> searchTagsPage(CmsTagsInfoDto cmsTagsInfoDto) {
        if (null == cmsTagsInfoDto) {
            return CommonResult.failed("未获取参数信息");
        }
        PageHelper.startPage(cmsTagsInfoDto.getPageNum(), cmsTagsInfoDto.getPageSize());
        CmsTagsInfo cmsTagsInfo = BeanUtil.copyProperties(cmsTagsInfoDto, CmsTagsInfo.class);
        List<CmsTagsInfo> cmsTagsInfos = cmsTagsInfoMapper.searchTags(cmsTagsInfo);
        if (cmsTagsInfos.isEmpty()) {
            CommonResult.failed("为查询到信息");
        }
        return CommonResult.success(CommonPage.restPage(cmsTagsInfos));
    }

    @Override
    @Transactional
    public CommonResult<Object> addTags(CmsTagsInfo cmsTagsInfo) {
        if (null == cmsTagsInfo) {
            return CommonResult.failed("未获取参数信息");
        }
        cmsTagsInfo.setStatus(1);
        cmsTagsInfo.setInsertDate(new Date());
        int insertSelective = cmsTagsInfoMapper.insertSelective(cmsTagsInfo);
        if (insertSelective < 0) {
            return CommonResult.failed("插入失败");
        }
        return CommonResult.success(cmsTagsInfo);
    }

    @Override
    @Transactional
    public CommonResult<Object> updateTags(CmsTagsInfo cmsTagsInfo) {
        if (null == cmsTagsInfo) {
            return CommonResult.failed("未获取参数信息");
        }
        int update = cmsTagsInfoMapper.updateByPrimaryKeySelective(cmsTagsInfo);
        if (update < 0) {
            return CommonResult.failed("更新失败");
        }
        return CommonResult.success(cmsTagsInfo);
    }

    @Override
    @Transactional
    public CommonResult<Object> delTags(CmsTagsInfo cmsTagsInfo) {
        if (null == cmsTagsInfo) {
            return CommonResult.failed("未获取参数信息");
        }
        cmsTagsInfo.setStatus(0);
        int update = cmsTagsInfoMapper.updateByPrimaryKeySelective(cmsTagsInfo);
        if (update < 0) {
            return CommonResult.failed("更新失败");
        }
        return CommonResult.success(cmsTagsInfo);
    }

    @Override
    @Transactional
    public CommonResult<Object> batchTags(Integer status, List<String> ids) {
        if (ids.isEmpty()) {
            return CommonResult.failed("操作失败");
        }
        int updateBatch = cmsTagsInfoMapper.batchTags(status, ids);
        if (updateBatch < 0) {
            return CommonResult.failed("更新失败");
        }
        return CommonResult.success(updateBatch);
    }
}
